<?php

class homeActions extends sfActions {

    public function executeIndex(sfWebRequest $request){
        /* inicializa con un idioma */
        if (!$request->getParameter('sf_culture')){
            if ($this->getUser()->isFirstRequest()){
                $culture = $request->getPreferredCulture(array('en', 'fr'));
                $this->getUser()->setCulture($culture);
                $this->getUser()->isFirstRequest(false);
            }else{
                $culture = $this->getUser()->getCulture();
            }

            $this->redirect('@localized_homepage');
        }
        /* end idioma */

        /* recepcion de criterios de busqueda */
        $category = $request->getParameter('ca');/* ca: category */
        $tipo = $request->getParameter('tipo');
        $country = $request->getParameter('c');/* c: country */
        $subcategory = $request->getParameter('sc');/* sc: subcategory */
        $time = $request->getParameter('t');/* t: time */
        $c = new Criteria();
        if($tipo!=''){
            if($tipo == '1'){/* leidos */
                $c_views = new Criteria();
                $c_views->addSelectColumn(ArticlePeer::ID);
                $c_views->addSelectColumn("COUNT(".ArticleViewsPeer::IP.") as views");
                $c_views->addDescendingOrderByColumn("views");
                $c_views->setLimit(10);
                $article_ids = ArticleViewsPeer::doSelectStmt($c_views);
                foreach($article_ids as $ai){
                    $art_ids[] = $ai[0];
                }
                $c->add(ArticlePeer::ID, $art_ids, Criteria::IN);
            }elseif($tipo == '2'){/* puntuados */
                $c_scores = new Criteria();
                $c_scores->addSelectColumn(ArticlePeer::ID);
                $c_scores->addSelectColumn("COUNT(".ArticleScoresPeer::SCORE.") as scores");
                $c_scores->addDescendingOrderByColumn("scores");
                $c_scores->setLimit(10);
                $article_ids = ArticleScoresPeer::doSelectStmt($c_scores);
                foreach($article_ids as $ai){
                    $art_ids[] = $ai[0];
                }
                $c->add(ArticlePeer::ID, $art_ids, Criteria::IN);
            }elseif($tipo == '3'){/* relevantes */
                //visitas - views
                "SELECT a.id as article_id, count(av.ip) as views
                FROM article a
                left join article_views av
                on a.id = av.article_id
                group by a.id";

                //payments - leidos
                "SELECT a.id as article_id, count(ap.account_id) as payments
                FROM article a
                left join article_payments ap
                on a.id = ap.article_id
                group by a.id";

                "SELECT tbl.article_id, views, count(ap.account_id) as payments
                FROM (
                  SELECT a.id as article_id, count(av.ip) as views
                  FROM article a
                  left join article_views av
                  on a.id = av.article_id
                  group by a.id
                ) as tbl
                left join article_payments ap
                on tbl.article_id = ap.article_id
                group by tbl.article_id";
                /* relevantes = leidos * vistos */
            }
        }
        if($category!=''){
            $c->add(ArticlePeer::CATEGORY_ID, $category);
        }
        if($country!=''){
            $obj_country = CountryPeer::retrieveByPK($country);
            $c->add(ArticlePeer::COUNTRY_ID, $obj_country->getId());
            $this->getUser()->setAttribute('scri_country',$obj_country);
        }else{
            if($this->getUser()->hasAttribute('scri_country')){
                $obj_country = CountryPeer::retrieveByPK($this->getUser()->getAttribute('scri_country')->getId());
                $c->add(ArticlePeer::COUNTRY_ID, $obj_country->getId());
                $this->getUser()->setAttribute('scri_country', $obj_country);
            }
        }
        if($subcategory!=''){
            $c->add(ArticlePeer::SUBCATEGORY_ID, $subcategory);
        }
        if($time!=''){
            $eight_left = mktime(date("H")-8, date("i"), date("s"), date("m"), date("d"), date("Y"));
            $day_left = mktime(date("H"), date("i"), date("s"), date("m"), date("d")-1, date("Y"));
            $week_left = mktime(date("H"), date("i"), date("s"), date("m"), date("d")-7, date("Y"));

            if($time=='0'){/* no elegido */
                /* no se hace nada */
            }elseif($time=='1'){/* 8 horas */
                $c->add(ArticlePeer::CREATED_AT, date("Y-m-d H:i:s",$eight_left), Criteria::GREATER_EQUAL);
            }elseif($time=='2'){/* 24 horas */
                $c->add(ArticlePeer::CREATED_AT, date("Y-m-d H:i:s",$day_left), Criteria::GREATER_EQUAL);
            }elseif($time=='3'){/* 168 horas(1 semana) */
                $c->add(ArticlePeer::CREATED_AT, date("Y-m-d H:i:s",$week_left), Criteria::GREATER_EQUAL);
            }
            $c->addDescendingOrderByColumn(ArticlePeer::CREATED_AT);
            $this->getUser()->setAttribute('scri_time',$time);
        }
        $c->addJoin(ArticlePeer::OWNER_ID, AccountPeer::ID);
        $c->addDescendingOrderByColumn(ArticlePeer::RANK);
        $c->add(ArticlePeer::ENABLED, 1);
        $c->add(ArticlePeer::IS_DRAFT, 0);
        $articles = ArticlePeer::doSelect($c);
        $this->articles = $articles;

        $comments_count = array();
        foreach($articles as $article){
            $c->clear();
            $c->add(ArticleCommentPeer::ARTICLE_ID, $article->getId());
            $comments_count[$article->getId()] = ArticleCommentPeer::doCount($c);
        }
        $this->comments_count = $comments_count;

        $ids = array();
        foreach($articles as $article){
            $ids[] = $article->getAccount()->getPractilAccountId();
        }

        //crear metodo que reciba ids y devuelva arreglo de usuarios practil
        $lib = new myPractil();
        $url = $lib->url_practil_get_users($ids);

        $myLib = new myLib();
        $array_users = array();
        $array_users = $myLib->getUsersFromPractil($ids);

        $this->array_users = $array_users;
        $this->subcategory = SubcategoryPeer::retrieveByPK($subcategory);
    }

    public function executeLoggedOut(sfWebRequest $request){
        /* */
    }

}